package com.fire.controller.impl;

import com.fire.controller.IStudentController;
import com.fire.controller.ITeacherController;
import com.fire.factory.Factory;
import com.fire.service.ITeacherService;
import com.fire.service.impl.TeacherServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Scanner;

public class TeacherControllerImpl implements ITeacherController {
    private static final Logger LOGGER= LoggerFactory.getLogger(TeacherControllerImpl.class);

    private ITeacherService teacherService = Factory.getTeacherServiceImpl();
    private DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    private Scanner sc = new Scanner(System.in);
    private IStudentController stuController =Factory.getStudentControllerImpl();
    @Override
    public void Login() throws IOException {
        String name = null;
        while (true) {
            System.out.println("请输入用户名");
            name = sc.nextLine();
            if(name!=null){
                break;
            }else {
                System.out.println("用户名不能为空");
            }
        }
        LocalDateTime now = LocalDateTime.now();
        String password = null;
        while (true) {
            System.out.println("请输入密码");
            password = sc.nextLine();
            if(password!=null){
                break;
            }else {
                System.out.println("密码不能为空");
            }
        }
        int flag = teacherService.Login(name, password);
        if (flag == -1) {
            System.out.println("该用户不存在");
        } else if (flag == -2) {
            System.out.println("暂无用户，清先注册");
        } else {
            LocalDateTime beforeTime = teacherService.isHaveChance(name, now);
            Duration duration = Duration.between(now, beforeTime);
            long minutes = duration.toMinutes();
            if (minutes > 40) {
                beforeTime = beforeTime.minusMinutes(40);
                String time = df.format(beforeTime);
                System.out.println("您已经在20分钟内连续3次账号或密码输入错误，您的下次可登录时间为：" + time);
            } else if (40 == minutes && flag == 0) {
                System.out.println("登陆失败，您在20分钟内不能再登录了");
            } else if (20 == minutes && flag == 0) {
                System.out.println("登陆失败，你还有" + 1 + "次机会");
            } else if (minutes == 0 && flag == 0) {
                System.out.println("登陆失败，你还有" + 2 + "次机会");
            } else {
                LocalDateTime time= teacherService.WriterAndClear(name,now);
                Duration dur = Duration.between(now, time);
                long min = dur.toMinutes();

                if(min==0){
                    System.out.println("欢迎您首次登录本系统，祝您有一个愉快的体验");
                    LOGGER.debug(name+"登陆成功");
                }else {
                    String time2 = df.format(time);
                    System.out.println("欢迎再次来到本系统，您上次的登录时间是："+time2);
                }
                stuController.start();

             //   System.out.println("登陆成功");
            }
        }
    }


    @Override
    public void Register() throws IOException {
        String id = null;
        while (true) {
            System.out.println("请输入ID");
            id = sc.nextLine();
            boolean flag=teacherService.isExist(id);
            if(flag==true){
                System.out.println("该用户ID已存在，请从新输入");
            }else {
                break;
            }
        }
        System.out.println("请输入密码");
        String p1 = sc.nextLine();
        System.out.println("请确认密码");
        String p2 = sc.nextLine();
        if(p1.equals(p2)){
            teacherService.Regirter(id,p1);
            System.out.println("注册成功，你可以使用该用户登录了");
        }else {
            System.out.println("密码错误，注册失败");
        }

    }

    @Override
    public LocalDateTime CloseTime(String name) throws IOException {//查询退出时间
        LocalDateTime time=teacherService.CloseTime( name);
        return time;
    }

    @Override
    public LocalDateTime RegisterTime(String name) throws IOException {//查询注册时间
        LocalDateTime time=teacherService.RegisterTime( name);
        return time;
    }

    @Override
    public void WriteCloseTime(String name) throws IOException {//写结束时间，系统退出时调用
        teacherService. WriteCloseTime(name);
    }


}
